package com.hdc.hdc.warehouseManage.utill;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;

import com.hdc.hdc.warehouseManage.exception.MyException;

/**
 * 文件操作工具
 * @author gjy
 *
 * 2018年3月2日上午11:12:27
 */
public class FileUtil {
	
	
	private static final Logger log = LoggerFactory.getLogger(FileUtil.class);

	
	//删除文件
	public static void deleteFileForWindows(String url) {
		
		if (IsNotNullValit.StringIsNotNull(url)) {
			
			log.info("执行删除的文件:"+url);
			
			File file = new File(url);
			
			if(file.exists()) {
				log.info("文件存在，执行删除；");
				file.delete();
				log.info("删除成功");
			}else {
				throw new MyException("不存在的文件");
			}
			
		}else {
			throw new MyException("不存在的地址");
		}
		
		
	}
	
	/** 
	 * 下载项目根目录下doc下的文件 
	 * @param response response 
	 * @param fileName 文件名 
	 * @return 返回结果 成功或者文件不存在 
	 */  
	public static void downloadFile(HttpServletResponse response, String fileName) {  
	    File path = null;  
	    response.setHeader("content-type", "application/octet-stream");  
	    response.setContentType("application/octet-stream");  
	    try {  
	        response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));  
	    } catch (UnsupportedEncodingException e2) {  
	        e2.printStackTrace();  
	    }  
	    byte[] buff = new byte[1024];  
	    BufferedInputStream bis = null;  
	    OutputStream os = null;       
	    try {  
	        path = new File(ResourceUtils.getURL("classpath:").getPath());  
	        os = response.getOutputStream();  
	        bis = new BufferedInputStream(new FileInputStream(new File(path + "/templates/excelmodual/" + fileName)));  
	        int i = bis.read(buff);  
	        while (i != -1) {  
	            os.write(buff, 0, buff.length);  
	            os.flush();  
	            i = bis.read(buff);  
	        }  
	    } catch (FileNotFoundException e1) {  
	        throw new MyException("系统找不到指定的文件");  
	    }catch (IOException e) {  
	        e.printStackTrace();  
	    } finally {  
	        if (bis != null) {  
	            try {  
	                bis.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	    }  
	}
	
	/** 
	 * 下载项目根目录下的文件 ,linux下
	 * @param response response 
	 * @param fileName 文件名 
	 * @return 返回结果 成功或者文件不存在 
	 */  
	public static void downloadFileInLinxu(HttpServletResponse response, String fileName,String systemType) {  
	    InputStream stream = FileUtil.class.getClassLoader().getResourceAsStream("doc/" + fileName);  
	    response.setHeader("content-type", "application/octet-stream");  
	    response.setContentType("application/octet-stream");  
	    try {  
	        String name = java.net.URLEncoder.encode(fileName, "UTF-8");  
	        response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLDecoder.decode(name, "ISO-8859-1") );  
	    } catch (UnsupportedEncodingException e2) {  
	        e2.printStackTrace();  
	    }  
	    byte[] buff = new byte[1024];  
	    BufferedInputStream bis = null;  
	    OutputStream os = null;       
	    try {  
	        os = response.getOutputStream();              
	        bis = new BufferedInputStream(stream);  
	        int i = bis.read(buff);  
	        while (i != -1) {  
	            os.write(buff, 0, buff.length);  
	            os.flush();  
	            i = bis.read(buff);  
	        }  
	    } catch (FileNotFoundException e1) {  
	        throw new MyException("系统找不到指定的文件");  
	    }catch (IOException e) {  
	        e.printStackTrace();  
	    } finally {  
	        if (bis != null) {  
	            try {  
	                bis.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();  
	            }  
	        }  
	    }  
	} 
	
	 // @描述：是否是2003的excel，返回true是2003   
    public static boolean isExcel2003(String filePath)  {    
        return filePath.matches("^.+\\.(?i)(xls)$");    
    }    
     
    //@描述：是否是2007的excel，返回true是2007   
    public static boolean isExcel2007(String filePath)  {    
        return filePath.matches("^.+\\.(?i)(xlsx)$");    
    }    
      
	  /** 
	   * 验证EXCEL文件 
	   * @param filePath 
	   * @return 
	   */  
	public static boolean validateExcel(String filePath) {
		if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
			return false;
		}
		return true;
	}  

}
